<template>
  <div class="me">
    <div class="header">
      <div class="portrait">
        <i class="iconfont icon-admin-user icon-size42"></i>
        <span class="font_bold" @click="changeShow" v-if="!isLogin"
          >点击登录</span
        >
        <span class="font_bold" v-else>{{ user.userName }}</span>
      </div>
    </div>
    <!-- 吸顶 -->
    <div class="card" :class="{ card_fixed: isFixed }">
      <img src="@/assets/img/vip.png" alt />
      <div class="tabs">
        <div class="tab" @click="unUse">
          <i class="iconfont icon-shoucang1 icon-size28"></i>
          <span>收藏</span>
        </div>
        <div class="tab" ref="vip" @click="unUse">
          <i class="iconfont icon-pingjia2 icon-size28"></i>
          <span>评价</span>
        </div>
        <div class="tab"  @click="unUse">
          <i class="iconfont icon-zuji icon-size28"></i>
          <span>足迹</span>
        </div>
        <div class="tab"  @click="toCoupon">
          <i class="iconfont icon-wodekajuan icon-size28"></i>
          <span>红包卡劵</span>
        </div>
      </div>
    </div>
    <!-- 非吸顶 -->
    <div :class="{ other_fixed: isFixed }">
      <div class="task"  @click="unUse">
        <img src="@/assets/img/task.png" alt />
      </div>

      <!-- 设置标签 -->
      <div class="body">
        <div
          class="body_card"
          v-for="(tab_value, tab_key) in tabs"
          :key="tab_key"
        >
          <van-cell
            v-for="tab in tab_value"
            :key="tab_key + '_' + tab.label"
            :to="tab.path"
            :title="tab.label"
            is-link
            :icon="tab.images"
          />
        </div>
      </div>
    </div>
    <!-- 遮罩层-登录 -->
    <ToLogin :show="show" @changeShow="changeShow"></ToLogin>
  </div>
</template>

<script>
import Vue from "vue";
import { Cell, CellGroup, Dialog } from "vant";
import ToLogin from "../../components/ToLogin";

Vue.use(Cell);
Vue.use(CellGroup).use(Dialog);
export default {
  name: "me",
  components: {
    ToLogin,
  },
  data() {
    return {
      scroll: "",
      me_scroll: null,
      isFixed: false,
      show: false,
      isLogin: null,
      tabs: {
        0: [
          {
            label: "意见反馈",
            images: require("@/assets/drawable-mdpi/question.png"),
            path: "/question",
          },
          {
            label: "联系客服",
            images: require("@/assets/drawable-mdpi/my-free.png"),
            path: "/service",
          },
          {
            label: "关于我们",
            images: require("@/assets/drawable-mdpi/about.png"),
            path: "/about",
          },
          {
            label: "个人资料",
            images: require("@/assets/drawable-mdpi/mine.png"),
            path: "/person",
          },
          {
            label: "我的设置",
            images: require("@/assets/drawable-mdpi/setting.png"),
            path: "/setting",
          },
          
        ],
      },
    };
  },
  watch: {
    user(newValue, oldVAlue) {
      console.log("触发登录状态监听", newValue);
      if (newValue != null) {
        this.isLogin = true;
      } else {
        this.isLogin = false;
      }
    },
    isLogin(newValue, oldValue) {
      console.log("isLogin监听", newValue);
      this.show = !newValue;
    },
  },
  computed: {
    user() {
      return this.$store.getters.userInfo;
    },
  },
  mounted() {
    // window.addEventListener("scroll", this.handleScroll); // 监听滚动事件，然后用handleScroll这个方法进行相应的处理
    window.addEventListener("scroll", this.handleScroll, true);
    this.$store.dispatch("setAction", 3);
    console.log("checkLogin", this.user);
    if (this.user != null) {
      this.isLogin = true;
    } else {
      this.isLogin = false;
    }
  },
  //销毁事件
  beforeDestroy() {
    // 离开该页面需要移除这个监听的事件，不然会报错  必须带第三个参数true，否则销毁不成功
    window.removeEventListener("scroll", this.handleScroll, true);
  },
  beforeRouteLeave(to, from, next) {
    console.log(to.fullPath);
    var path = to.fullPath;
    if (
      path == "/order" ||
      path == "/store" ||
      path == "/home" ||
      path == "/commodity" ||
      path == "/login"
    ) {
      next();
    } else if (this.isLogin) {
      console.log("用户登录，跳转");
      next();
    } else {
      console.log("用户未登录，不跳转");
      this.show = true;
    }
  },
  methods: {
    changeShow() {
      if (this.show) {
        this.show = false;
      } else {
        this.show = true;
      }
    },
    handleScroll() {
      let oneRem = document.documentElement.offsetWidth / 10;
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop; // 滚动条偏移量
      //   console.log("偏移量：", scrollTop / oneRem);
      //   let offsetTop = this.$refs.vip.offsetTop; // 要滚动到顶部吸附的元素的偏移量

      if (!this.isFixed && scrollTop / oneRem >= 2.1875) {
        this.isFixed = true;
      } else if (this.isFixed && scrollTop / oneRem <= 2.1875) {
        this.isFixed = false;
      }
    },
    //还未开发
    unUse() {
      Dialog.alert({
        message: "该内容还未开发，请稍后。。。",
      }).then(() => {
        // on close
      });
    },
    toCoupon(){
      this.$router.push({ path: "/coupon" })
    }
  },
};
</script>

<style scoped>
.me {
  width: 100%;
  height: 100%;
  background-image: linear-gradient(to top, #f0f0f0, white);
  overflow-x: hidden;
  overflow-y: scroll;
  -ms-overflow-style: none;
  overflow: -moz-scrollbars-none;
  scrollbar-width: none;
}
.me::-webkit-scrollbar {
  display: none;
  width: 0 !important;
  height: 0;
}
.icon-size42 {
  font-size: 38px;
  color: #646566;
}
.icon-size28 {
  font-size: 28px;
  color: #646566;
}
.font_bold {
  font-weight: bold;
  font-size: 18px;
}
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 2.13rem;
  box-sizing: border-box;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  z-index: 2;
}
.portrait {
  display: flex;
  flex-direction: row;
  height: 1rem;
  line-height: 1rem;
  font-size: 16px;
  margin: 1rem 0.5rem 0 0.5rem;
}
.portrait span {
  margin-left: 0.4rem;
}
/* 吸顶效果 */
.card_fixed {
  position: fixed;
  top: -2.185rem;
  left: 0;
  right: 0;
  z-index: 1;
}
.other_fixed {
  margin-top: 6.53rem;
}
/* 非吸顶效果 */
.card {
  display: flex;
  flex-direction: column;
  min-height: 2.3rem;
  margin-top: 2.63rem;
  box-shadow: 0px 3px 3px #e1e1e1;
}
.card img {
  width: 100%;
  height: 2rem;
}
.tabs {
  width: 100%;
  min-height: 1.5rem;
  display: flex;
  flex-direction: row;
  justify-content: center;
}
.tab {
  height: 100%;
  width: 2.5rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  background-color: #fff;
  padding-bottom: 0.3rem;
}
.tab span {
  margin-top: 0.3rem;
}
.task {
  background-color: #fff;
}
.task img {
  width: 100%;
}
/* 单元格 */
.body {
  margin-bottom: 1.4rem;
}
.body_card {
  margin-top: 10px;
  background-color: #ffffff;
  /* border: 0.15px solid #66343434 ; */
}
.van-cell__right-icon {
  color: #59333333;
  font-weight: bolder;
}
.van-icon__image {
  width: 24px;
  height: 24px;
}
.van-cell__title {
  font-size: 16px;
  color: #333333;
  padding-left: 14.5px;
}
.van-cell {
  padding: 15px;
}
.van-cell:not(:last-child)::after {
  left: 58px;
  /* border-bottom: 1px solid rgba(51, 51, 51, 0.15); */
}
</style>